package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.service.CartService;
import com.changgou.order.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 购物车 控制层
 * @author 20201117
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 将商品信息，添加 到购物车
     * @param skuId 商品id
     * @param num 商品数量
     * @return
     */
    @GetMapping("/add")
    @ResponseBody
    public Result add(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num){
        //String username = "gaixiaoqi";
        String username = tokenDecode.getUserInfo().get("username");
        cartService.add(skuId,num,username);
        return new Result(true, StatusCode.OK,"添加购物车成功");
    }

    /**
     * 查询 用户 购物车信息
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<Map> list(){
        //String username = "gaixiaoqi";
        String username = tokenDecode.getUserInfo().get("username");
        Map resultMap = cartService.list(username);
        return new Result(true,StatusCode.OK,"查询购物车信息成功",resultMap);
    }


}
